﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using WeChat.Common;
using BLL;

namespace WeChat.Manager
{
    public partial class KefuManages : System.Web.UI.Page
    {
        SetKefu sk = new SetKefu();
        KefuBLL kb = new KefuBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!ForLogin.IsLogin())
                {
                    Response.Redirect("../Login.aspx"); return;
                }
                Label3.Text = "";
            }
        }
        //新增客服
        protected void addKf(object sender, EventArgs e)
        {
            string kfaccount = Request["accounts"].ToString();
            string kfnick = Request["nick"].ToString();
            string password = Request["password"].ToString();
            if (kfaccount == "" || kfnick == "" || password == "") { return; }
            //else { return; }
            string m = sk.setNewKefu(kfaccount,kfnick,password);
            Label1.Text = m;
            shows(sender, e);
            Page.ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>tips();</script>");
        }
        //修改客服
        protected void alterKf(object sender, EventArgs e)
        {
            string kfaccount = Request["accountsAt"].ToString();
            string kfnick = Request["nicksAt"].ToString();
            string password = Request["passwordAt"].ToString();
            if (kfaccount == "" || kfnick == "" || password == "") { return; }
            //else { return; }
            string m = sk.updateKefu(kfaccount, kfnick, password);
            Label1.Text = m;
            shows(sender, e);
            Page.ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>tips();</script>");
        }
        //查看所有客服
        protected void shows(object sender, EventArgs e)
        {
            showKefus.DataSource = kb.getKefu();
            showKefus.DataBind();
        }
        //同步数据，重设密码
        protected void Restart_syn(object sender, EventArgs e)
        {
            string newPwd = Request["newpwd"].ToString();
            if (newPwd == "") { return; }
            //else { return; }
            int i = sk.getKefu(newPwd);
            Label1.Text = "同步数据成功！";
            shows(sender,e);
            Page.ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>tips();</script>");
        }
        //删除客服
        protected void showKefus_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                string accounts = e.CommandArgument.ToString();
                string m = sk.delKefu(accounts);
                shows(source, e);
                Label1.Text = m;
                Page.ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>tips();</script>");
            }
        }
        //查看在线客服
        protected void showkfOL(object sender, EventArgs e)
        {
            DataTable dt = sk.getKefuStates();
            Repeater1.DataSource = dt;
            Repeater1.DataBind();
            if (dt.Rows.Count > 0)
            {
                Label3.Text = "";
            }
            else
            {
                Label3.Text = "*当前无客服在线！";
            }
        }


    }
}
